"use strict";

import {
    //核心
    React, Enhance, Controller, $,
    tools,
    // 组件
    Table, TabPage, Lang, Dialog, Tree,
    Form, Page, Upload, Icon, Grid, Panel,
    Input, BoxWidget, Button, Lock, Directive
} from 'admin';

import valid from '../../validate/admin.json';

var vDate;

class ApkController extends Controller {
    state = {
        tableProps: null,
        mreq: {
            "name": "required",
            "version": "required",
            "url": "required"
        },
        edit: {}
    }


    constructor() {
        super();
        this.state.tableProps = {
            update: {},
            query: {},
            buttons: {
                edit: {
                    onClick: this._handlerEditDialog
                },
                upload: this._handlerCreateUpload,
                'delete': {
                    onClick: this._handlerDeleteDialog
                }
            },
            pk: 'id',
            fields: [
                {
                    order: -1,
                    name: 'id',
                },
                {
                    order: 0,
                    name: 'name',
                },
                {
                    order: 0,
                    name: 'version',
                },
                {
                    order: 0,
                    name: 'url',
                },
                {
                    order: 0,
                    name: 'remark',
                },
                {
                    order: 0,
                    name: 'time',
                    type: {
                        name: 'datetime'
                    }
                },

            ]
        };
    }


    render() {
        var {edit} = this.state;
        return (
            <Grid>
                <Grid col="12 12 12 12">
                    <Form {...this.props} onSubmit={this._handlerSearch}>
                        <BoxWidget color="4" buttons={[{title: 'search', icon: 'search', type: 'submit'}]}>
                            <Grid col="2 2 2 2"><Input type="text" name="version"
                                                       message={this._lang("version")}></Input></Grid>
                        </BoxWidget>
                    </Form>
                </Grid>
                <Grid col="12 12 12 12">
                    <BoxWidget color="3" buttons={[{icon: 'add', title: 'add', onClick: this._handlerAddDialog}]}>
                        <Lock>
                            <Table ref="table" {...this.props} {...this.state.tableProps} ></Table>
                        </Lock>
                    </BoxWidget>
                </Grid>

                {/*添加*/}
                <Directive name="Modal">
                    <Dialog
                        ref="addDialog"
                        elem={Form}
                        {...this.props}
                        action="do_add"
                        title={this._lang("add")}
                        onSuccess={this._handlerAddSuccess}
                        rules={this.state.mreq}
                        buttons={[{title: 'submit', type: 'submit'}]}
                    >
                        <Input type="text" name="name" message={this._lang("name")}></Input>
                        <Input type="text" name="version" message={this._lang("version")}></Input>
                        <Input type="text" name="url" message={this._lang("url")}></Input>
                        <Input type="text" name="remark" message={this._lang("remark")}></Input>
                    </Dialog>
                </Directive>
                {/*编辑*/}
                <Directive name="Modal">
                    <Dialog
                        ref="editDialog"
                        elem={Form}
                        {...this.props}
                        action="do_edit"
                        title={this._lang("edit")}
                        onSuccess={this._handlerEditSuccess}
                        rules={this.state.mreq}
                        buttons={[{title: 'submit', type: 'submit'}]}
                    >
                        <Input type="hidden" name="id" value={edit.id}></Input>
                        <Input type="text" name="name" message={this._lang("name")} value={edit.name}></Input>
                        <Input type="text" name="version" message={this._lang("version")} value={edit.version}></Input>
                        <Input type="text" name="url" message={this._lang("url")} value={edit.url}></Input>
                        <Input type="text" name="remark" message={this._lang("remark")} value={edit.remark}></Input>
                    </Dialog>
                </Directive>
                <Directive name="Modal">
                    <Dialog
                        ref="deleteDialog"
                        elem={Form}
                        {...this.props}
                        action="do_delete"
                        title={this._lang('delete')}
                        rules={{}}
                        buttons={[{title:'submit',type:'submit'}]}
                        onSuccess={this._handlerDeleteSuccess.bind(this)}
                    >
                        <input type="hidden" name="id" value={edit.id}/>
                    </Dialog>
                </Directive>
            </Grid>
        );
    }

    /**
     *
     * @param delete
     * @private
     */
    _handlerDeleteDialog(edit) {
        this.setState({edit}, (function () {
            this.refs.deleteDialog.show();
        }).bind(this));
    }

    /**
     *
     * @param data
     * @private
     */
    _handlerDeleteSuccess(data) {
        if (data) {
            this.refs.table.refresh(data);
            this.refs.deleteDialog.hide();
            window.toast(<Lang lang="message.success.delete"/>);
        } else {
            window.toast(<Lang lang="message.error.delete"/>);
        }
    }

    /**
     *
     * @param data
     */
    _handlerEditDialog(edit) {
        this.setState({edit}, (function () {
            this.refs.editDialog.show();
        }).bind(this));
    }

    _handlerCreateUpload(key, data, index) {
        return (
            <Upload
                size="40M"
                filter={['apk']}
                url={window.SERVER_URL+"/scdc/upload/get_apk"}
                onSuccess={this._handlerUploadSuccess}
                post={{id:1}}
            ><Icon icon="upload" type="admin"></Icon>
            </Upload>
        );
    }

    /**
     *
     * @param data
     * @private
     */
    _handlerUploadSuccess(data) {
        if (data) {
            this.refs.table.update(data);
            window.toast(<Lang lang="scdc.apk.success"/>);
        } else {
            window.toast(<Lang lang="scdc.apk.error"/>);
        }
    }

    /**
     *
     * @private
     */
    _handlerAddDialog() {
        this.refs.addDialog.show();
    }

    /**
     *
     * @param data
     * @private
     */
    _handlerEditSuccess(data) {
        if (data) {
            this.refs.table.update(data);
            this.refs.editDialog.hide();
        }
    }

    /**
     *
     * @param data
     * @private
     */
    _handlerAddSuccess(data) {
        if (data) {
            this.refs.table.refresh();
            this.refs.addDialog.hide();
        }
    }


    /**
     *
     * @param data
     * @returns {boolean}
     * @private
     */
    _handlerSearch(data) {
        this.refs.table.query(data);
        return false;
    }

}

export default Enhance(ApkController);